package com.chenyue.cm.utils;

import java.io.IOException;
import java.util.Map;

import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;
/**
 * 简单封装 freemarker
 * @author liangc
 *
 */
public class FreeMarkerUtils {
	
	/**
	 * 
	 * @param templateName
	 * @param paramMap
	 * @return
	 * @throws TemplateNotFoundException
	 * @throws MalformedTemplateNameException
	 * @throws ParseException
	 * @throws IOException
	 * @throws TemplateException
	 */
	public static String formatSql(String templateName, Map<String, Object> paramMap) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		return getTemplateContent(templateName,paramMap);
	}
	
	/**
	 * 根据模板路径加载sql
	 * @param templateName
	 * @param paramMap
	 * @return
	 * @throws TemplateNotFoundException
	 * @throws MalformedTemplateNameException
	 * @throws ParseException
	 * @throws IOException
	 * @throws TemplateException 
	 */
	@SuppressWarnings("deprecation")
	public static String getTemplateContent(String templateName, Map<String, Object> paramMap) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		Configuration cfg = new Configuration();
		cfg.setClassForTemplateLoading(FreeMarkerUtils.class, "/template/ftl");
		Template t = cfg.getTemplate(templateName); // freeMarker template
		//生成sql语句
		String sqlString = FreeMarkerTemplateUtils.processTemplateIntoString(t, paramMap);
		return sqlString;
	}
	
	
}
